# -*- mode: python -*-

import os
import SCons

Import( "env conf" )


display = SCons.Util.DisplayEngine()

opts_file = "customopts.py"

if os.path.exists( File( opts_file ).abspath ):
    display( "Reading options from \"%s\"" % File( opts_file ).abspath )

opts = Options( opts_file )



opts.AddOptions(
    ( "DEBUG", "Turn on debug messages if > 0", 0 ),
    ( "CPPFLAGS", "C Pre-Processor flags", "" ),
    BoolOption( "static", "Build static binaries", 0 ),
    PackageOption( "sdl",
                   "use sdl installed here (yes = search some places)",
                   "yes" ),
    BoolOption( "save_options",
                "Save choosen options to file.",
                0 ),
    )

opts.Update( env )
if env[ "DEBUG" ]:
    env.Append( CPPFLAGS=" -g" )
    env.Append( CPPDEFINES={ "DEBUG": env[ "DEBUG" ] } )

if env[ "CPPFLAGS" ]:
    env.Append( CPPFLAGS=env[ "CPPFLAGS" ] )

if env[ "sdl" ] == 1:
    if not conf.CheckLib( "libSDL" ) or not conf.CheckHeader( "SDL.h" ):
        # get sdl info using sdl-config
        static = ""
        if env[ "static" ]:
            static = " --static-libs"
        env.ParseConfig( "sdl-config --cflags --libs %s" % ( static, ) )
        if "SDL" in env[ "LIBS" ] or "libSDL" in env[ "LIBS" ]:
            env.Append( CPPDEFINES={ "HAVE_SDL": 1 } )
    else:
        env.Append( CPPDEFINES={ "HAVE_SDL": 1 } )

if env[ "static" ]:
    env.Append( LINKFLAGS="-static" )

## Keep this at the end!
opts.Update( env )

save_opts = env[ "save_options" ]
if save_opts:
    display( "Saving options to \"%s\"" % File( opts_file ).abspath )
    opts.Save( opts_file, env )
# end if save options

Help( opts.GenerateHelpText( env ) )


